home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1989 / 05 / timer.pas < prev    next >
Pascal/Delphi Source File  |  1989-08-03  |  3KB  |  59 lines

  1. Turbo Pascal high-resolution timer example. This timer reads the DOS time-of-
  2. day data word and thus extends the range of the timer beyond 54 milliseconds.
  3. Interrupts must be enabled during timing. The calibration routine, as well
  4. as the routine to compute elapsed time between two high-resolution timestamps,
  5. is left to the reader.
  6.  
  7. tphrt_type = record
  8.                 ticks : word;               { 838 ns 8253 timer ticks     }
  9.                 tocks : word;               { BIOS TOD 54.925 timer tocks }
  10.              end;
  11.  
  12. procedure t_start;
  13. {----------------------------------------------------------------------------
  14.  |  This procedure initializes the PC 8253 timer chip to mode 2 and calls   |
  15.  |  the timer-calibration routine.                                          |
  16.  ----------------------------------------------------------------------------}
  17. begin
  18.     inline(
  19.             $B0/$34/                        { mov  al,00110100b }
  20.             $E6/$43/                        { out  43h,al       }
  21.             $2B/$C0/                        { sub  ax,ax        }
  22.             $E6/$40/                        { out  40h,al       }
  23.             $E6/$40                         { out  40h,al       }
  24.           );
  25.     t_calib;                                { call the calibration routine }
  26. end;  { t_start }
  27.  
  28. procedure t_get(var timestamp : tphrt_type);
  29. {----------------------------------------------------------------------------
  30.  |  This procedure grabs the current counts from the 8253 timer and the     |
  31.  |  BIOS time-of-day clock. Interrupts are off during the timer reads,      |
  32.  |  then back on.                                                           |
  33.  ----------------------------------------------------------------------------}
  34. var
  35.     bios_tod : word absolute $0040:$006C;
  36. begin
  37.     inline($FA);                            { interrupts off }
  38.     port[$43] := 0;
  39.     timestamp.ticks := port[$40];
  40.     timestamp.ticks := timestamp.ticks + (port[$40] shl 8);
  41.     timestamp.tocks := bios_tod;
  42.     inline($FB);                            { interrupts on }
  43. end; { t_get }
  44.  
  45. procedure t_stop;
  46. {----------------------------------------------------------------------------
  47.  |  This procedure disables 8253 mode 2 and sets 8253 mode 3 counting,      |
  48.  |  which is what the 8253 was doing before we meddled with it. . . .       |
  49.  |  Invoke when all timings complete.                                       |
  50.  ----------------------------------------------------------------------------}
  51. begin
  52.     inline(
  53.             $B8/$36/                        { mov ax,00110110b }
  54.             $E7/$43/                        { out 43h,ax       }
  55.             $2B/$C0/                        { sub ax,ax        }
  56.             $E6/$40/                        { out 40h,al       }
  57.             $E6/$40                         { out 40h,al       }
  58.           );
  59. end; { t_stop }